﻿using System;
using System.Drawing;
using System.Drawing.Design;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.Design;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace BySoft.Web.Controls
{
    [Designer(" BySoft.Web.Controls.UploadControlDesigner")]
    public class UploadControl : FileUpload, INamingContainer
    {
        public event EventHandler Uploaded;
        
        protected ImageButton btnReturn = new ImageButton();
        protected LinkButton lnkReturn = new LinkButton();//该控件为了迫使页面能够生成__EVENTTARGET
        //private string text = "";

        private void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            if (this.Uploaded != null)
                this.Uploaded(this, e);
        }

        private string strOnUploadedScript
        {
            get
            {
                return @"
			
				function " + this.ClientID + @"_Upload_OnUpload()
				{
                " + (this.Uploaded != null ? @"
				try{
					var submitObject = '';
					var theform;
					if (window.navigator.appName.toLowerCase().indexOf('netscape') > -1) {
						theform = document.forms['" + this.Page.Form.ClientID + @"'];
					}
					else {
						theform = document." + this.Page.Form.ClientID + @";
					}
					theform.__EVENTTARGET.value = '" + this.btnReturn.UniqueID + @"';
					theform.__EVENTARGUMENT.value = '';

					theform.submit();
				}
				catch(exception){} " : "") + @"
				}
			";
            }
        }

        private const string moveControlScript = @"
  
   function moveUploadfile(obj){  /*
    eventX = event.srcElement.offsetLeft ;
    eventY = event.srcElement.offsetTop ;

	var y = eventY - (obj.offsetHeight -event.srcElement.offsetHeight) / 2;
    var x = eventX - (obj.offsetWidth -event.srcElement.offsetWidth)  / 2;
	*/

    eventX = event.x;
    eventY = event.y;

    var y = eventY - obj.offsetHeight / 2;
    var x = eventX - obj.offsetWidth / 2;
            
    obj.style.posLeft = x  ;
    obj.style.posTop = y;
   }      
   ";

        private void InitControls()
        {
            this.lnkReturn.Style["display"] = "none";
            //this.btnReturn.Style["display"] = "none";
            this.btnReturn.ID = "btnReturn";
            this.btnReturn.Click += new ImageClickEventHandler(btnReturn_Click);

            this.Style.Add("position", "absolute");
            this.Style.Add("filter", "alpha(opacity=0)");
            this.Style.Add("width", "30px");
            this.Style.Add("cursor", "pointer");

            this.Controls.Add(btnReturn);
            this.Controls.Add(lnkReturn);
        }

        protected override void OnLoad(EventArgs e)
        {
            //Page.RegisterClientScriptBlock(this.ClientID + "_OnUploaded", strOnUploadedScript);
            //Page.RegisterClientScriptBlock("UploadControl_MoveControl", moveControlScript);
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), this.ClientID + "_OnUploaded", strOnUploadedScript,true);
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "UploadControl_MoveControl", moveControlScript,true);

            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            this.btnReturn.Attributes["onmouseover"] = "moveUploadfile(" + this.ClientID + ")";
            this.btnReturn.Attributes["onclick"] = "return false;";
            this.btnReturn.CssClass = this.CssClass;
            this.btnReturn.Width = this.Width;
            this.btnReturn.Height = this.Height;
            this.btnReturn.Visible = this.Visible;

            this.Attributes["onchange"] = this.ClientID + @"_Upload_OnUpload()";

            base.OnInit(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            
            base.Render(writer);
        }

        ///// <summary>
        ///// 获取或设置按钮标题
        ///// </summary>
        //public string Text
        //{
        //    get
        //    {
        //        return btnReturn.Text;
        //    }
        //    set
        //    {
        //        btnReturn.Text = value;
        //    }
        //}

        /// <summary>
        /// 获取或设置按钮图片对齐方式
        /// </summary>
        public ImageAlign ImageAlign
        {
            get
            {
                return btnReturn.ImageAlign;
            }
            set
            {
                btnReturn.ImageAlign = value;
            }
        }

        /// <summary>
        /// 获取或设置图片的位置
        /// </summary>
        [Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), DefaultValue(""), Bindable(true)]
        public string ImageUrl
        {
            get
            {
                return btnReturn.ImageUrl;
            }
            set
            {
                btnReturn.ImageUrl = value;
            }
        }

        public UploadControl() : base()
        {
            InitControls();
        }

    }

    public class UploadControlDesigner : ControlDesigner
    {
        public override string GetDesignTimeHtml()
        {
            return @"
				<INPUT type=""image"" style=""WIDTH: " + ((UploadControl)this.Component).Width + @";HEIGHT: " + ((UploadControl)this.Component).Height + @""" src=""" + ((UploadControl)this.Component).ImageUrl + @""">
				";
        }

    }
}
